package com.liohi.Param;

import com.liohi.AllEunm.ResponseStatus;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    private boolean success;
    private Integer code;
    private String message;
    private Map<String,Object>data=new HashMap<>();

    private R(){}

    /**
     * 成功，无数据，默认消息
     * @return
     */
    public static R ok(){
        R r=new R();
        r.setSuccess(true);
        r.setCode(ResponseStatus.SUCCESS.getCode());
        r.setMessage("成功");
        return r;
    }

    /**
     * 成功，无数据,自定义成功消息
     * @param msg
     * @return
     */
    public static R ok(String msg) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResponseStatus.SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }

    /**
     * 成功，接收Map类型参数，将数据封装的data中
     * @param map
     * @return
     */
    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResponseStatus.SUCCESS.getCode());
        r.setMessage("成功");
        r.data.putAll(map);
        return r;
    }

    /**
     * 成功，以键值对形式接收参数，将数据封装的data中
     * @param key
     * @param value
     * @return
     */
    public static R ok(String key, Object value) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResponseStatus.SUCCESS.getCode());
        r.setMessage("成功");
        r.data.put(key, value);
        return r;
    }

    /**
     * 失败，无数据，默认状态码，默认消息
     * @return
     */
    public static R error(){
        R r=new R();
        r.setSuccess(false);
        r.setCode(ResponseStatus.ERROR.getCode());
        r.setMessage("失败");
        return r;
    }

    /**
     * 失败，无数据，默认状态码，自定义失败消息
     * @param msg
     * @return
     */
    public static R error( String msg) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResponseStatus.ERROR.getCode());
        r.setMessage(msg);
        return r;
    }

    /**
     * 失败，无数据，自定义状态码，自定义失败消息
     * @param code
     * @param msg
     * @return
     */
    public static R error(int code, String msg) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }
}
